package cn.tqfeiyang.smartadmin.business.organization.validator;

import cn.tqfeiyang.smartadmin.business.organization.entity.EmployeeEntity;
import cn.tqfeiyang.smartadmin.business.organization.mapper.EmployeeMapper;
import cn.tqfeiyang.smartadmin.commons.mybatis.IEntityValidator;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import cn.hutool.core.lang.*;
import cn.tqfeiyang.smartadmin.commons.exception.*;
import cn.tqfeiyang.smartadmin.commons.toolbox.*;
import cn.hutool.core.lang.Validator;
import cn.tqfeiyang.smartadmin.business.organization.mapper.DepartmentMapper;

/**
 * 员工实体验证器
 *
 * @author tqfeiyang
 * @since 2024-10-18
 */
@Component
public class EmployeeValidator implements IEntityValidator<EmployeeEntity> {

    /**
     * 员工映射接口
     */
    @Resource
    private EmployeeMapper employeeMapper;

    /**
     * 部门映射接口
     */
    @Resource
    private DepartmentMapper departmentMapper;

    /**
     * 验证实体数据
     *
     * @param entity 员工实体
     * @param creating 是否是添加
     */
    @Override
    public void validate(EmployeeEntity entity, boolean creating) {
        if (StringUtils.isNotBlank(entity.getLoginName())) {
            // 检查登录账号是否重复？
            if (employeeMapper.checkLoginNameExists(entity.getLoginName(), entity.getEmployeeId())) {
                throw new BusinessException("登录账号重复({})", entity.getLoginName());
            }
        }
        if (StringUtils.isNotBlank(entity.getEmail())) {
            // 检查电子邮件是否重复？
            if (employeeMapper.checkEmailExists(entity.getEmail(), entity.getEmployeeId())) {
                throw new BusinessException("电子邮件重复({})", entity.getEmail());
            }
            // 检查电子邮件是否为邮箱地址？
            if (!Validator.isEmail(entity.getEmail())) {
                throw new BusinessException("电子邮件不是正确的邮箱地址({})", entity.getEmail());
            }
        }
        if (StringUtils.isNotBlank(entity.getPhone())) {
            // 检查电话是否重复？
            if (employeeMapper.checkPhoneExists(entity.getPhone(), entity.getEmployeeId())) {
                throw new BusinessException("电话重复({})", entity.getPhone());
            }
            // 检查电话是否为手机号码（中国）？
            if (!Validator.isMobile(entity.getPhone())) {
                throw new BusinessException("电话不是正确的手机号码({})", entity.getPhone());
            }
        }
        if (NumberUtils.nonZero(entity.getDepartmentId())) {
            // 检查部门id是否存在？
            if (!departmentMapper.isExists(entity.getDepartmentId())) {
                throw new BusinessException("部门id不存在({})", entity.getDepartmentId());
            }
        }
    }

    /**
     * 删除前检查
     *
     * @param entity 员工实体
     */
    @Override
    public void beforeDelete(EmployeeEntity entity) {
        // 检查是否有地址存在？
        if (employeeMapper.countOfAddressesByEmployeeId(entity.getEmployeeId()) > 0) {
            throw new BusinessException("有地址存在，不能删除");
        }
    }

    /**
     * 删除后处理
     *
     * @param entity 员工实体
     */
    @Override
    public void afterDelete(EmployeeEntity entity) {
    }
}